
const allAlbum = '全部'
const allAlbumId = 'all'
var curAlbum = allAlbum;
var curAlbumId = allAlbumId;

if(curAlbumId === allAlbumId){
	$('#trigInput').addClass('sr-only');
}else {
	$('#trigInput').removeClass('sr-only');
}

//获取相册列表
function getAlbumList(){
	$.ajax({
		url: 'album/list',
		type: 'POST',
		success: function(response){
			if(response.status === '0'){
				let allClassName = '';
				if(curAlbumId === allAlbumId){
					allClassName = 'nav-link active';
				}else {
					allClassName = 'nav-link';
				}
				$('.albumList').empty().append(`
					<li class="nav-item"><a class="${allClassName}" href="javascript:;" data-id="${allAlbumId}">${allAlbum}</a></li>
				`);
				response.data.forEach(function(item, i){
					let itemClassName = '';
					if(curAlbumId === item.id){
						itemClassName = 'nav-link active'
					} else {
						itemClassName = 'nav-link'
					}
					$('.albumList').append(`
						<li class="nav-item">
							<a class="${itemClassName}" href="javascript:;" data-id="${item.id}">${item.name}</a>
							<button type="button" class="close" aria-label="Close" data-id="${item.id}">
	  							<span aria-hidden="true">&times;</span>
							</button>
						</li>
					`);
					
				})
			}
		}
	})
}
getAlbumList();

//获取某个相册下的数据
function getAlbumImageList(id){
	$.ajax({
		url: '/album/image/list',
		type: 'POST',
		data: {id},
		success: function (response){
			if(response.status === '0'){
				$('.imageList').empty();
				if(response.data.length === 0){
					$('.imageList').append('暂无图片');
					return;
				}
				response.data.forEach(function(item, i){
					$('.imageList').append(`
						<div class="col-3" data-id="${item._id}">
							<img src="${item.path}" class="rounded">
							<button type="button" class="close" aria-label="Close">
	  							<span aria-hidden="true" data-id="${item._id}">&times;</span>
							</button>
						</div>
					`);
				})
			}
		}
	})
}
getAlbumImageList(curAlbumId);

//更改相册
function changeAlbum(newAlbumId, newAlbum){
	curAlbumId = newAlbumId;
	curAlbum = newAlbum;

	if(curAlbumId === allAlbumId){
		$('#trigInput').addClass('sr-only');
	}else {
		$('#trigInput').removeClass('sr-only');
	}

	$('.albumList').find('a').removeClass('active');
	$('.albumList a[data-id=' + curAlbumId + ']').addClass('active');
	getAlbumImageList(curAlbumId);
}

$('.albumList').on('click', 'a', function(){
	event.preventDefault();
	//console.log(event.target);
	changeAlbum($(event.target).attr('data-id'), $(event.target).html());
	//getAlbumImageList()
})


//打开创建相册模态框
$('.openCreateAlbumModalBtn').click(function(){
	$('#createAlbumModal input').val('');
	$('#createAlbumModal').modal('show');
})


//创建相册
$('.createAlbumBtn').click(function(){
	let name = $('#createAlbumModal input').val();
	$.ajax({
		url: '/album/create',
		type: 'POST',
		data: {name},
		success: function(response){
			if(response.status === '0'){
				getAlbumList();
				$('#createAlbumModal').modal('hide');
			}else if(response.status === '-1'){
				alert(response.message);
			}
		}
	})
})

//删除相册
$('.albumList').on('click', '.close', function(){
	event.preventDefault();
	let albumId = $(event.target).parent().attr('data-id');
	let albumName = $(event.target).parent().prev('a').html();
	//console.log(albumId, albumName)
	let r = confirm('是否确认删除相册' + albumName);
	if(r){
		$.ajax({
			url: '/album/delete',
			type: 'GET',
			data: {albumId},
			success: function(response){
				if(response.status === '0'){
					if(albumName === curAlbum){
						curAlbum = allAlbum;
					}
					getAlbumList();
					getAlbumImageList(curAlbum);
				}
			}
		})
	}
})


//触发选择图片
$('#trigInput').click(function(){
	$('#selectImage').click();
	
})
$('#selectImage').change(function(){
	let input = event.target
	console.log(input.files[0]);
	let formData = new FormData();
	formData.append('myfile', input.files[0]);
	formData.append('albumId', curAlbumId);
	console.log(formData)
	$.ajax({
		url: '/album/image/add',
		type: 'POST',
		data: formData,
		// 告诉jQuery不要去处理发送的数据
		processData : false,
		cache: false,
		contentType: false,
		// contentType: 'multipart/form-data',
		success: function(response){
			if(response.status === '0'){
				$('#selectImage').val(null);
				getAlbumImageList(curAlbumId);
			}
		}
	})
	/*fetch('/album/image/add', {
	    method: 'POST',
	    // headers: {
	    //   "Content-Type": "multipart/form-data"
	    // },
	    body: formData
	}).then(function(res){
	    return res.json();
	}).then(function(res){
	    if(res.status == 0){
	    }else if(res.status == -2){

	    } else{

	    }
	})*/
})


//删除图片
$('.imageList').on('click', '.close', function(){
	// event.preventDefault();
	// console.log(event.target);
	var id = $(event.target).attr('data-id');
	$.ajax({
		url: '/album/image/delete',
		type: 'get',
		data: {id},
		success: function(response){

		}
	})
})